// 站点配置相关功能模块

// 加载站点配置
function loadSiteConfig() {
    fetch('../BlogMake.php', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: 'action=get_site_config'
    })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                const config = data.data || {};
                document.getElementById('site-title').value = config.title || '';
                document.getElementById('site-description').value = config.description || '';
                document.getElementById('site-author').value = config.author || '';
                document.getElementById('site-keywords').value = config.keywords || '';
                document.getElementById('site-logo').value = config.logo || '';
                document.getElementById('site-url').value = config.url || '';
                document.getElementById('site-about').value = config.about || '';
                document.getElementById('site-interests').value = config.interests || '';
                document.getElementById('site-self-introduction').value = config.selfIntroduction || '';
                document.getElementById('site-skills').value = config.skills || '';

                // 加载分区列表
                loadPartitionList();
            } else {
                showStatus('加载站点配置失败: ' + data.message, 'error');
            }
        })
        .catch(error => {
            console.error('加载站点配置失败:', error);
            showStatus('加载站点配置失败', 'error');
        });
}

// 保存站点配置
function saveSiteConfig() {
    // 安全获取DOM元素值，添加空值检查
    const getElementValue = (id, defaultValue = '') => {
        const element = document.getElementById(id);
        return element ? element.value : defaultValue;
    };
    
    const title = getElementValue('site-title');
    const subtitle = getElementValue('site-subtitle');
    const description = getElementValue('site-description');
    const author = getElementValue('site-author');
    const footer = getElementValue('site-footer');
    const keywords = getElementValue('site-keywords');
    const analytics = getElementValue('site-analytics');
    const copyright = getElementValue('site-copyright');
    const avatar = getElementValue('site-avatar');
    const banner = getElementValue('site-banner');
    const background = getElementValue('site-bg');
    const favicon = getElementValue('site-favicon');
    const header = getElementValue('site-header');
    const navbar = getElementValue('site-navbar');
    const sidebar = getElementValue('site-sidebar');
    const postsPerPage = getElementValue('site-posts-per-page');
    const rss = getElementValue('site-rss');
    const social = getElementValue('site-social');
    const theme = getElementValue('site-theme');
    const url = getElementValue('site-url');
    const lang = getElementValue('site-lang');

    // 构建配置对象
    const config = {
        title,
        subtitle,
        description,
        author,
        footer,
        keywords,
        analytics,
        copyright,
        avatar,
        banner,
        background,
        favicon,
        header,
        navbar,
        sidebar,
        postsPerPage: postsPerPage ? parseInt(postsPerPage) : 10,
        rss,
        social,
        theme,
        url,
        lang
    };

    fetch('../BlogMake.php', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: 'action=save_site_config&config=' + encodeURIComponent(JSON.stringify(config))
    })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                showStatus('站点配置保存成功', 'success');
            } else {
                showStatus('保存站点配置失败: ' + data.message, 'error');
            }
        })
        .catch(error => {
            console.error('保存站点配置失败:', error);
            showStatus('保存站点配置失败', 'error');
        });
}